package com.yanqu.road.server.manger.activity.channelmusicroomrank;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.able.ActivityAbleInfo;
import com.yanqu.road.entity.activity.channelmusicroomrank.ChannelMusicRoomRankDetailConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.channelmusicroomrank.ChannelMusicRoomRankConfig;
import com.yanqu.road.logic.thread.ThreadTaskManger;
import com.yanqu.road.pb.activity.ChannelMusicRoomRankProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.channelmusicroom.ChannelMusicRoomRankActivityModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;

import com.yanqu.road.server.manger.activity.able.ActivityAbleMgr;
import com.yanqu.road.server.manger.cross3.ChannelRankActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelMusicRoomRankActivityMgr extends TempMgr {

    public static ChannelMusicRoomRankConfig config = null;
    private static ThreadTaskManger threadTaskManger;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        if (null == threadTaskManger) {
            threadTaskManger = new ThreadTaskManger(1, "ChannelMusicRoomRankActivityMgr");
        }
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static Map<String,ChannelMusicRoomRankProto.ChannelMusicRoomRankConfigMsg.Builder> configMap = new ConcurrentHashMap<>();

    public static void reloadActivity(){
        getLogger().info("reload ChannelMusicRoomRank Activity start");
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.ChannelMusicRoomRank.getValue());
        configMap = new ConcurrentHashMap<>();
        if (list.isEmpty()){
            config = null;
            getLogger().info("reload ChannelMusicRoomRank Activity end,no activity in show time");
            return;
        }

        ActivityInfo activityInfo = list.get(0);
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        if (activityConfigMap == null || activityConfigMap.isEmpty()){
            config = null;
            getLogger().error("reload ChannelMusicRoomRank Activity error,config error");
            return;
        }

        config = new ChannelMusicRoomRankConfig(activityInfo,activityConfigMap);
        getLogger().info("reload ChannelMusicRoomRank Activity end,activityId:{}",activityInfo.getActivityId());

        addUserActivityAbleListener();

        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()){
            gamePlayer.getModule(ChannelMusicRoomRankActivityModule.class).checkAndSendData();
        }

    }

    public static ChannelMusicRoomRankConfig getConfig() {
        return config;
    }

    public static boolean isInActivityTime() {
        if (config == null) {
            return false;
        }
        return ActivityMgr.activityInTime(config.getActivityInfo());
    }

    public static boolean isInActivityShow() {
        if (config == null) {
            return false;
        }
        return ActivityMgr.activityInShowTime(config.getActivityInfo());
    }

    public static int getActivityId() {
        if (config == null) {
            return 0;
        }
        return config.getActivityInfo().getActivityId();
    }


    public static ChannelMusicRoomRankProto.ChannelMusicRoomRankConfigMsg.Builder getConfigMsg(String language){
        if(config==null){
            return null;
        }
        if (configMap.containsKey(language)){
            return configMap.get(language);
        }else {
            synchronized (configMap){
                if (!configMap.containsKey(language)){
                    ChannelMusicRoomRankProto.ChannelMusicRoomRankConfigMsg.Builder builder = initConfigMsg(language);
                    configMap.put(language,builder);
                }
            }
            return configMap.get(language);
        }



    }

    public static ChannelMusicRoomRankProto.ChannelMusicRoomRankConfigMsg.Builder initConfigMsg(String language) {
        ChannelMusicRoomRankProto.ChannelMusicRoomRankConfigMsg.Builder builder = ChannelMusicRoomRankProto.ChannelMusicRoomRankConfigMsg.newBuilder();
        builder.setActivityId(getActivityId());

        for (ChannelMusicRoomRankDetailConfig configRankData : config.getRankConfigList()) {
            ChannelMusicRoomRankProto.ChannelMusicRoomRankConfigTemp.Builder temp = ChannelMusicRoomRankProto.ChannelMusicRoomRankConfigTemp.newBuilder();
            temp.setId(configRankData.getId());
            temp.setName(ServerLanguageMgr.getContent(configRankData.getName(), language));
            temp.setRank(configRankData.getRank());
            temp.setScore(configRankData.getScore());
            temp.setReward(configRankData.getReward());
            temp.setDesc(ServerLanguageMgr.getContent(configRankData.getDesc(), language));
            temp.setIcon(configRankData.getIcon());
            builder.addConfigData(temp);
        }
        return builder;
    }

    public static void addTask(int round, int theme, Map<Long, List<ChannelMusicRoomRankProto.ChannelMusicRoomRankShowTemp>> userResultMap) {
        if (threadTaskManger != null) {
            threadTaskManger.addTask(0, new ChannelMusicRoomRankActivityTask(round, theme, userResultMap));
        }
    }

    /**
     * 添加活动资格监听 跨域特殊增加
     */
    private static void addUserActivityAbleListener() {
        if (config == null) {
            getLogger().error("addUserActivityAbleListener error,config is null");
            return;
        }

        Map<Integer, ActivityAbleInfo> ableInfoMap = new ConcurrentHashMap<>();

        ActivityAbleInfo ableInfo = new ActivityAbleInfo();
        ActivityInfo info = config.getActivityInfo();
        ableInfo.setActivityInfo(info);
        ableInfo.setAbleItemId(Integer.parseInt(info.getParam4()));

        ableInfoMap.put(info.getActivityId(), ableInfo);

        // 添加监听
        ActivityAbleMgr.addActivityAbleListener(eActivityType.ChannelMusicRoomRank.getValue(), ableInfoMap,
                (tmpAbleInfo, userId) -> {
                    // 排行榜变更
                    ChannelRankActivityMgr.refreshRankValue(tmpAbleInfo.getActivityInfo().getActivityId(), userId);
                }
        );


    }
}
